from typing import Dict, Any

import matplotlib.pyplot as plt
from fastapi import APIRouter, Depends, Response

from backend.config.settings import settings
from backend.utils.data_analysis import DataAnalysis
from backend.utils.permission import require_admin

router = APIRouter(
prefix="/admin/analysis",
    tags=["admin-analysis"],
    dependencies=[Depends(require_admin)],  # 管理员专属权限
    responses={401: {"description": "未授权"}, 403: {"description": "禁止访问"}}
)

data = DataAnalysis(file_path=settings.DATASET_PATH)
plt.rcParams["font.family"] = "SimHei"

@router.get("/patient_features", response_model=Dict[str, Any])
async def patient_features():
    """获取患者特征"""
    return data.get_patient_features()

@router.get("/disease_distribution", response_model=Dict[str, float])
async def disease_distribution():
    """获取疾病分布"""
    return data.get_disease_distribution()

@router.get("/age_disease_correlation", response_model=Dict[str, float])
async def age_disease_correlation():
    """获取年龄疾病关系"""
    return data.age_disease_correlation()

@router.get("/age_chart")
async def age_chart():
    """生成年龄分布图表"""
    buffer = data.generate_age_distribution_chart()
    return Response(content=buffer.read(), media_type="image/png")

@router.get("/disease_chart")
async def disease_chart():
    """生成疾病分布图表"""
    buffer = data.generate_disease_distribution_chart()
    return Response(content=buffer.read(), media_type="image/png")